import 'dart:async';

import 'package:flutter/material.dart';
import 'package:my_flutter_github/common/dao/UserDao.dart';
import 'package:my_flutter_github/model/User.dart';
import 'package:my_flutter_github/model/UserEvent.dart';
import 'package:my_flutter_github/model/UserOrg.dart';
import 'package:my_flutter_github/utils/NavigatorUtils.dart';
import 'package:my_flutter_github/utils/UserEventUtils.dart';
import 'package:my_flutter_github/widgets/BaseListState.dart';
import 'package:my_flutter_github/widgets/UserEventItemWidget.dart';
import 'package:my_flutter_github/widgets/UserHeaderItemWidget.dart';
import 'package:my_flutter_github/widgets/UserItemWidget.dart';

class BasePersonState<T extends StatefulWidget> extends BaseListState<T> {

  final List<UserOrg> orgList = List();

  @protected
  renderItem(index, User userInfo, String beStaredCount, Color notifyColor, VoidCallback refreshCallBack, List<UserOrg> orgList) {
    if (index == 0) {
      return new UserHeaderItemWidget(userInfo, beStaredCount, Theme.of(context).primaryColor,
          notifyColor: notifyColor, refreshCallBack: refreshCallBack, orgList: orgList);
    }

    print("userInfo.type:${userInfo.type}");

    if (userInfo.type == "Organization") {
      return new UserItemWidget(UserItemViewModel.fromMap(pullLoadWidgetControl.dataList[index - 1]), onPressed: () {
        NavigatorUtils.goPerson(context, UserItemViewModel.fromMap(pullLoadWidgetControl.dataList[index - 1]).userName);
      });
    } else {
      var event = pullLoadWidgetControl.dataList[index - 1];
      return new UserEventItemWidget(EventViewModel.fromEventMap(event), onPressed: () {
        UserEventUtils.ActionUtils(context, event, "");
      });
    }
  }

  @override
  bool get wantKeepAlive => true;

  @override
  bool get autoRefresh => true;

  @override
  bool get needHeader => true;

  @protected
  getUserOrg(String userName) {
    if(pageIndex <= 1) {
      UserDao.getUserOrgsDao(userName, pageIndex, needDb: true).then((result) {
          if(result != null && result.result) {
            setState(() {
              orgList.clear();
              orgList.addAll(result.data);
            });
            return result.next;
          }
          return Future.value(null);
      }).then((result) {
        if(result != null && result.result) {
          setState(() {
            orgList.clear();
            orgList.addAll(result.data);
          });
        }
      });
    }
  }
}